package com.evildog.workspace.auth.grant;


import cn.hutool.core.util.StrUtil;
import com.evildog.workspace.common.util.SpringContextHolder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrantFactory {

    /**
     * TokenGranter缓存池
     */
    private static final Map<String, Granter> granterMap = new ConcurrentHashMap<>(16);

    static {
        granterMap.put("default", SpringContextHolder.getBean(PasswordTokenGranter.class));
        granterMap.put(PasswordTokenGranter.GRANT_TYPE, SpringContextHolder.getBean(PasswordTokenGranter.class));
        granterMap.put(RefreshTokenGranter.GRANT_TYPE, SpringContextHolder.getBean(RefreshTokenGranter.class));
    }

    /**
     * 获取TokenGranter
     *
     * @param grantType 授权类型
     * @return ITokenGranter
     */
    public static Granter getGranter(String grantType) {
        Granter granter = granterMap.get(StrUtil.toString(grantType));
        return granter == null ? granterMap.get("default") : granter;
    }

}
